import cgi
import os 
from google.appengine.ext import db
from google.appengine.ext.webapp import template
from google.appengine.ext import webapp                                        
from google.appengine.ext.webapp.util import run_wsgi_app                      
import logging                                                                 
 
from StringIO import StringIO                                                  
import traceback
import simplejson
import xmlrpclib
from xmlrpcserver import XmlRpcServer
from google.appengine.api import users
from entities import *

#--------------------------------------
#-- JSON
#--------------------------------------
class RPCHandler(webapp.RequestHandler):
    
    def __init__(self):
        webapp.RequestHandler.__init__(self)
        self.methods = RPCMethods()
    
    def get(self):
        func = None

        action = self.request.get("action")
        if action:
            func = getattr(self.methods,action, None)
        if not func:
            self.error(404) #file not found
        args = ()
        while True:
            key = 'arg%d' %len(args)
            val = self.request.get(key)
            if val:
                args += (simplejson.loads(val),)
            else:
                break
        result = func(*args)
        self.response.out.write(simplejson.dumps(result))
    
class RPCMethods:
    def Add(self,*args):
        ints = [int(arg) for arg in args]
        return sum(ints)
    
    def Gettest(self, *args):
        #test= [{'company': 'FPT', 'phone': '01663355355','lat':'21.034797','lng': '105.770187'},
         #      {'company': 'Chua Boc', 'phone': '01663335252','lat':'21.009319' ,'lng':'105.824089'},
         #    {'company': 'Ho Tay company', 'phone': '01363335252','lat':'21.064115','lng': '105.823917'}]
        
        test = NhaTuyenDung.GetLocation(args[0], args[1], args[2], args[3])       
        return simplejson.dumps(test)
    
    
